#coding:utf-8
from django.db import models
from pythonems.models import ISQL, SQL_M2M, response_format
from pythonems.docs import IMongo
from major.models import Major, Char
import copy

class IType(ISQL):
    class Meta:
        permissions = (
            ("get_itype", "Get or Search IType"),
        )
        ordering = ["pk"]
    major = models.ManyToManyField(Major, through="IType_Major")
    is_hat = models.BooleanField(default=False)
    detail = models.CharField(max_length=255)
    # ########
    # CRUD
    # ########
    relation_dict = copy.copy(ISQL.relation_dict)
    relation_dict.update({
        "is_hat": "is_hat",
        "detail": "detail",
    })
    values_args = copy.copy(ISQL.values_args)
    values_args.extend([
        "is_hat", "detail",
    ])
    create_arg_list = copy.copy(ISQL.create_arg_list)
    create_arg_list.extend([
        "is_hat", "detail",
    ])
    update_arg_list = copy.copy(ISQL.update_arg_list)
    update_arg_list.extend([
        "is_hat", "detail",
    ])
    search_arg_dict = {
        "pk_list": "pk__in",
    }
class IType_Major(SQL_M2M):
    class Meta:
        permissions = (
            ("get_itype_major", "Get or Search IType_Major"),
        )
        ordering = ["pk"]
    major = models.ForeignKey(Major)
    itype = models.ForeignKey(IType)
    # ########
    # CRUD
    # ########
    main_key = "major_id"
    another_key = "itype_id"
    another_key_list = "itype_id_list"
    relation_dict = {
        "pk": "id",
        "major_id": "major_id",
        "itype_id_list": "itype_id_list",
    }
class Level(ISQL):
    class Meta:
        permissions = (
            ("get_level", "Get or Search Level"),
        )
        ordering = ["pk"]
class Detail(IMongo):
    __collection__ = 'question'
    structure = copy.copy(IMongo.structure)
    structure.update({
        'body': basestring,
        'image': basestring,
        'answer': basestring,
        'analysis': basestring,
    })
    default_values = {
        'body': "",
        'image': "",
        "answer": "",
        "analysis": "",
    }
    relation_dict = copy.copy(IMongo.relation_dict)
    relation_dict.update({
        'body': 'body',
        'image': 'image',
        'answer': 'answer',
        'analysis': 'analysis',
    })        
class Question(ISQL):
    class Meta:
        permissions = (
            ("get_question", "Get or Search question"),
        )
        ordering = ["pk"]
    major = models.ForeignKey(Major)
    char = models.ManyToManyField(Char, through="Question_Char")
    itype = models.ForeignKey(IType)
    parent = models.ForeignKey("self", null=True)
    level = models.ForeignKey(Level)
    opt_len = models.PositiveSmallIntegerField(default=0)
    detail_id = models.CharField(max_length=40)
    # ########
    # CRUD
    # ########
    relation_dict = copy.copy(ISQL.relation_dict)
    relation_dict.update({
        "major_id": "major_id",
        "itype_id": "itype_id",
        "parent_id": "parent_id",
        "level_id": "level_id",
        "opt_len": "opt_len",
    })
    values_args = copy.copy(ISQL.values_args)
    values_args.extend([
        "major_id", "itype_id", "parent_id", "level_id", "opt_len",
    ])
    create_arg_list = copy.copy(ISQL.create_arg_list)
    create_arg_list.extend([
        "major_id", "itype_id", "parent_id", "level_id", "opt_len",
    ])
    update_arg_list = copy.copy(ISQL.update_arg_list)
    update_arg_list.extend([
        "major_id", "itype_id", "parent_id", "level_id", "opt_len",
    ])
    update_forginkey_dict = {"major_id": "major",
                             "itype_id": "itype",
                             "parent_id": "parent",
                             "level_id": "level",}
    search_arg_dict = copy.copy(ISQL.search_arg_dict)
    search_arg_dict.update({
        "major_id": "major_id",
        "char_id_list": "char__in",
        "itype_id": "itype_id",
        "parent_id": "parent_id",
        "level_id": "level_id",
        "opt_len": "opt_len",
    })
class Question_Char(SQL_M2M):
    class Meta:
        permissions = (
            ("get_question_char", "Get or Search Question_Char"),
        )
        ordering = ["pk"]
    question = models.ForeignKey(Question)
    char = models.ForeignKey(Char)
    # ########
    # CRUD
    # ########
    main_key = "question_id"
    another_key = "char_id"
    another_key_list = "char_id_list"
    relation_dict = {
        "pk": "id",
        "question_id": "question_id",
        "char_id_list": "char_id_list",
    }
